clear *
insheet using "data_to_post.csv"

// make IDs unique across waves
replace id=id+wave*1000000


// drop participants who did not complete the study
preserve
collapse (mean) completed (count) rating, by(id)
sum completed
table rating completed
restore
drop if completed==0
// summarize demos
replace age=. if age<18 | age>90
sum age
tabulate gender
sum white

// fix coding error for covid news
replace covid_news =5 if covid_news ==7

// condense accimp variables across waves
replace accimp=v41 if v41~=.
replace accimp=v50 if v50~=.

// make gender dummies
gen male=gender==1
gen genderOther=gender>2 

// make condition dummies
gen accT=treatment==0                   // accuracy only
gen askingacc =treatment==2             // evaluation 
gen askingaccW2 =treatment==2 & wave==2 
gen askingaccW3 =treatment==2 & wave==3
gen frank=treatment==3                  // long evaluation
gen norms=treatment==4					// generic norms
gen tipsW3=treatment==5 & wave==3		// tips
gen tipsW4=treatment==5 & wave==4
gen norms2=treatment==4.5				// partisan norms
gen normstips=treatment==6				// tips+norms
gen impacc=treatment==7					// importance
gen imp_norm=treatment==8 				// importance+norms

// make overall treatment dummy
gen treated=treatment>1

replace treatment=99 if treatment==4.5

// create interaction variables
gen rXaccT=real*accT
gen rXaskingacc=real*askingacc
gen rXaskingaccW2 =real*askingaccW2
gen rXaskingaccW3 =real*askingaccW3
gen rXfrank=real*frank
gen rXnorms=real*norms
gen rXtipsW3=real*tipsW3
gen rXtipsW4=real*tipsW4
gen rXnorms2=real*norms2
gen rXnormstips=real*normstips
gen rXimpacc=real*impacc
gen rXimp_norm=real*imp_norm
gen rXtreated=real*treated

// make interactions using real as the holdout instead of fake
gen fake=1-real
gen fXaccT=fake*accT
gen fXaskingaccW2 =fake*askingaccW2
gen fXaskingaccW3 =fake*askingaccW3
gen fXfrank=fake*frank
gen fXnorms=fake*norms
gen fXtipsW3=fake*tipsW3
gen fXtipsW4=fake*tipsW4
gen fXnorms2=fake*norms2
gen fXnormstips=fake*normstips
gen fXimpacc=fake*impacc
gen fXimp_norm=fake*imp_norm
gen fXtreated=fake*treated



//////////////////
// MAIN EFFECT ANALYSIS


// main model
xi: cluster2 rating real accT rXaccT askingaccW2 rXaskingaccW2 askingaccW3 rXaskingaccW3 frank rXfrank norms rXnorms norms2 rXnorms2 tipsW3 rXtipsW3 tipsW4 rXtipsW4 normstips rXnormstips impacc rXimpacc imp_norm rXimp_norm  i.wave, tcluster(id) fcluster(item_num)
disp _b[askingaccW2]*7900/(7900+10800)+_b[askingaccW3]*10800/(7900+10800)
test askingaccW2*7900/(7900+10800)+askingaccW3*10800/(7900+10800)=0
disp _b[rXaskingaccW2]*7900/(7900+10800)+_b[rXaskingaccW3]*10800/(7900+10800)
test rXaskingaccW2*7900/(7900+10800)+rXaskingaccW3*10800/(7900+10800)=0


disp _b[tipsW3]*9960/(9960+8160)+_b[tipsW4]*8160/(9960+8160)
test tipsW3*9960/(9960+8160)+tipsW4*8160/(9960+8160)=0
disp _b[rXtipsW3]*9960/(9960+8160)+_b[rXtipsW4]*8160/(9960+8160)
test rXtipsW3*9960/(9960+8160)+rXtipsW4*8160/(9960+8160)=0

//   bootstrapped SEs for askingacc and tips
xi: bootstrap aa=(_b[askingaccW2]*7900/(7900+10800)+_b[askingaccW3]*10800/(7900+10800)) t=(_b[tipsW3]*9960/(9960+8160)+_b[tipsW4]*8160/(9960+8160)) rXaa=(_b[rXaskingaccW2]*7900/(7900+10800)+_b[rXaskingaccW3]*10800/(7900+10800)) rXt=(_b[rXtipsW3]*9960/(9960+8160)+_b[rXtipsW4]*8160/(9960+8160)), reps(1000) cluster(id):  cluster2 rating real accT rXaccT askingaccW2 rXaskingaccW2 askingaccW3 rXaskingaccW3 frank rXfrank norms rXnorms norms2 rXnorms2 tipsW3 rXtipsW3 tipsW4 rXtipsW4 normstips rXnormstips impacc rXimpacc imp_norm rXimp_norm  i.wave, tcluster(id) fcluster(item_num)
/* boot results
------------------------------------------------------------------------------
             |   Observed   Bootstrap                         Normal-based
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          aa |  -.0259955   .0148732    -1.75   0.080    -.0551464    .0031554
           t |   -.035669   .0143447    -2.49   0.013    -.0637842   -.0075539
        rXaa |   .0356939   .0095029     3.76   0.000     .0170686    .0543192
         rXt |     .03497   .0096905     3.61   0.000     .0159769    .0539632
*/
		 


//// COMPARISON ACROSS TREATMENTS
// everything better than norms
test rXaskingaccW2*7900/(7900+10800)+rXaskingaccW3*10800/(7900+10800)=rXnorms
test rXfrank=rXnorms
test rXtipsW3*9960/(9960+8160)+rXtipsW4*8160/(9960+8160)=rXnorms
test rXimpacc=rXnorms
test rXimp_norm=rXnorms
test rXaskingaccW2*7900/(7900+10800)+rXaskingaccW3*10800/(7900+10800)=rXnorms2
test rXfrank=rXnorms2
test rXtipsW3*9960/(9960+8160)+rXtipsW4*8160/(9960+8160)=rXnorms2
test rXimp_norm=rXnorms2
//        except impacc and norms2, p=.3
test rXimpacc=rXnorms2
// two different versions of norms arent different
test rXnorms=rXnorms2

// effective treatments comparison
//    long evaluation btter than most others
test rXfrank=rXimp_norm
test rXaskingaccW2*7900/(7900+10800)+rXaskingaccW3*10800/(7900+10800)=rXfrank
test rXimpacc=rXfrank
test rXnormstips=rXfrank
test rXtipsW3*9960/(9960+8160)+rXtipsW4*8160/(9960+8160)=rXfrank
//     no sig diffs between other effective treatments
test rXaskingaccW2*7900/(7900+10800)+rXaskingaccW3*10800/(7900+10800)=rXtipsW3*9960/(9960+8160)+rXtipsW4*8160/(9960+8160)
test rXaskingaccW2*7900/(7900+10800)+rXaskingaccW3*10800/(7900+10800)=rXimpacc
test rXtipsW3*9960/(9960+8160)+rXtipsW4*8160/(9960+8160)=rXimpacc
test rXaskingaccW2*7900/(7900+10800)+rXaskingaccW3*10800/(7900+10800)=rXnormstips
test rXtipsW3*9960/(9960+8160)+rXtipsW4*8160/(9960+8160)=rXnormstips
test rXaskingaccW2*7900/(7900+10800)+rXaskingaccW3*10800/(7900+10800)=rXimp_norm
test rXtipsW3*9960/(9960+8160)+rXtipsW4*8160/(9960+8160)=rXimp_norm
test rXnormstips=rXimp_norm
//       except impacc doing worse than thses
test rXimpacc=rXimp_norm
test rXnormstips=rXimpacc

// to get treatment effect on real w SEs for Fig 2a
xi: cluster2 rating fake accT fXaccT askingaccW2 fXaskingaccW2 askingaccW3 fXaskingaccW3 frank fXfrank norms fXnorms norms2 fXnorms2 tipsW3 fXtipsW3 tipsW4 fXtipsW4 normstips fXnormstips impacc fXimpacc imp_norm fXimp_norm  i.wave, tcluster(id) fcluster(item_num)
disp _b[askingaccW2]*7900/(7900+10800)+_b[askingaccW3]*10800/(7900+10800)
test askingaccW2*7900/(7900+10800)+askingaccW3*10800/(7900+10800)=0
disp _b[tipsW3]*9960/(9960+8160)+_b[tipsW4]*8160/(9960+8160)
test tipsW3*9960/(9960+8160)+tipsW4*8160/(9960+8160)=0
//   bootstrapped SEs for askingacc and tips
xi: bootstrap aa=(_b[askingaccW2]*7900/(7900+10800)+_b[askingaccW3]*10800/(7900+10800)) t=(_b[tipsW3]*9960/(9960+8160)+_b[tipsW4]*8160/(9960+8160)) fXaa=(_b[fXaskingaccW2]*7900/(7900+10800)+_b[fXaskingaccW3]*10800/(7900+10800)) fXt=(_b[fXtipsW3]*9960/(9960+8160)+_b[fXtipsW4]*8160/(9960+8160)), reps(1000) cluster(id):  cluster2 rating fake accT fXaccT askingaccW2 fXaskingaccW2 askingaccW3 fXaskingaccW3 frank fXfrank norms fXnorms norms2 fXnorms2 tipsW3 fXtipsW3 tipsW4 fXtipsW4 normstips fXnormstips impac fXimpacc imp_norm fXimp_norm  i.wave, tcluster(id) fcluster(item_num)
/* boot results

------------------------------------------------------------------------------
             |   Observed   Bootstrap                         Normal-based
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          aa |   .0096984   .0149818     0.65   0.517    -.0196654    .0390623
           t |   -.000699   .0145591    -0.05   0.962    -.0292342    .0278362
        fXaa |  -.0356939   .0097764    -3.65   0.000    -.0548552   -.0165326
         fXt |    -.03497      .0099    -3.53   0.000    -.0543738   -.0155663
------------------------------------------------------------------------------
*/



// to get means and CIs for fig 1
xi: bysort real accT: cluster2 rating if wave==1, tcluster(id) fcluster(item_num)




// OVERALL EFFECT COLLAPSING AROSS EFFECTIVE TREATMENTS
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.fake i.treated  i.fXtreated  i.wave  if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)




// Item-level analysis for Fig 3
preserve
statsby _b, by(item_num) clear : reg rating i.treated  i.wave  if treatment >0 & norms==0 & norms2==0 
save tmp, replace
restore
preserve
statsby _b, by(item_num) clear : reg rating if treatment ==0 
rename _b_cons percAcc
merge 1:1 item_num using tmp
scatter _stat_2 percAcc
	
restore







///////////////
// Moderation?

// first, confirm there's no sig treatment effects on the moderators
preserve
collapse (mean) treated covid_* accimp crtacc attention demrep_c  if treatment >0 & norms==0 & norms2==0 , by(id study )
xi: reg covid_concern_1 treated i.study
xi: reg covid_news treated i.study
xi: reg accimp treated i.study
xi: reg crtacc treated i.study
xi: reg attention treated i.study
xi: reg demrep_c treated i.study
restore


// ATTENTIVENESS MODERATION?
egen zattention=std(attention) if treatment>0
xi:  cluster2 rating i.real*zattention i.treated*zattention i.rXtreated*zattention i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
gen t_att = _b[_IrXtXzatte_1]/_se[_IrXtXzatte_1]
gen p_att = 2*ttail(e(df_r),abs(t_att))
// by response
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if attention ==0 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if attention ==1 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if attention ==2 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if attention ==3 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)


// CRT MODERATION?
egen zcrt=std(crtacc) if treatment>0
xi: cluster2 rating i.real*zcrt i.treated*zcrt i.rXtreated*zcrt i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
gen t_crt = _b[_IrXtXzc]/_se[_IrXtXzc]
gen p_crt = 2*ttail(e(df_r),abs(t_crt))
// by response level
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if crtacc  ==0 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if crtacc  ==1 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if crtacc  ==2 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if crtacc  ==3 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if crtacc  ==4 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if crtacc  ==5 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if crtacc  ==6 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)

// ACCIMP MODERATION?
egen zaccimp=std(accimp) if treatment>0
xi: cluster2 rating i.real*zaccimp i.treated*zaccimp i.rXtreated*zaccimp i.wave if treatment >0 & norms==0 & norms2==0  , tcluster(id) fcluster(item_num)
gen t_accimp = _b[_IrXtXza]/_se[_IrXtXza]
gen p_accimp = 2*ttail(e(df_r),abs(t_accimp ))
//     without accimp asking conditions
xi: cluster2 rating i.real*zaccimp i.treated*zaccimp i.rXtreated*zaccimp i.wave if treatment >0 & norms==0 & norms2==0  & treatment<7, tcluster(id) fcluster(item_num)
// by response
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if accimp ==1 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if accimp ==2 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if accimp ==3 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if accimp ==4 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if accimp ==5 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)

// COVID NEWS SEEKING MODERATION?
egen zcovid_news=std(covid_news) if treatment>0
xi: cluster2 rating i.real*zcovid_news i.treated*zcovid_news i.rXtreated*zcovid_news i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
gen t_cn = _b[_IrXtXzc]/_se[_IrXtXzc]
gen p_cn = 2*ttail(e(df_r),abs(t_cn))
// by response level
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if covid_news   ==1 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if covid_news   ==2 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if covid_news   ==3 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if covid_news   ==4 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if covid_news   ==5 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)

// COVID CONCERN MODERATION?
egen zcovid_concern=std(covid_concern_1) if treatment>0
xi: cluster2 rating i.real*zcovid_concern i.treated*zcovid_concern i.rXtreated*zcovid_concern i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
gen t_cc = _b[_IrXtXzc]/_se[_IrXtXzc]
gen p_cc = 2*ttail(e(df_r),abs(t_cc))


// DEM/REP MODERATION?
egen zdemrep=std(demrep_c) if treatment>0
xi: cluster2 rating i.real*zdemrep i.treated*zdemrep i.rXtreated*zdemrep i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
gen t_dr = _b[_IrXtXzd]/_se[_IrXtXzd]
gen p_dr = 2*ttail(e(df_r),abs(t_dr))


// DEMOGRAPHIC MODERATION?
gen age2=age^2
xi: cluster2 rating i.real*age i.real*age2 i.treated*age i.treated*age2 i.rXtreated*age i.rXtreated*age2  i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
test _IrXtXage_1 _IrXtXage2_1
//xi: cluster2 rating i.real*age  i.treated*age  i.rXtreated*age  i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
//gen t_age= _b[_IrXtXage_1 ]/_se[_IrXtXage_1 ]
gen p_age = r(p)
gen ageD=round(age/10)*10
preserve
xi: statsby _b _se, by(ageD) clear: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if treatment >0 & norms==0 & norms2==0 & norms==0 & norms2==0 & norms==0 & norms2==0, tcluster(id) fcluster(item_num)
gen low=_b_IrXtreated_1-1.96*_se_IrXtreated_1
gen hi=_b_IrXtreated_1+1.96*_se_IrXtreated_1
gen lowR=_b_Ireal-1.96*_se_Ireal
gen hiR=_b_Ireal+1.96*_se_Ireal
drop if ageD>80
twoway (scatter _b_IrXtreated_1 ageD) (rcap low hi ageD, ytitle("Treatment effect on Discernment") xtitle("Age") legend(off) name(treat))
twoway (scatter _b_Irea ageD) (rcap lowR hiR ageD, ytitle("Baseline Discernment") xtitle("Age") legend(off) name(disc))
restore
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if age>=18 & age<35 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if age>=35 & age<=50 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if age>50 & age<65 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if age>=65 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)


xi: cluster2 rating i.real*college i.treated*college i.rXtreated*college i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
gen t_ed = _b[_IrXtXc]/_se[_IrXtXc]
gen p_ed = 2*ttail(e(df_r),abs(t_ed))
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if college   ==0 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
xi: cluster2 rating i.real  i.treated  i.rXtreated  i.wave  if college   ==1 & treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)

xi: cluster2 rating i.real*white i.treated*white i.rXtreated*white i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
gen t_white= _b[_IrXtXw]/_se[_IrXtXw]
gen p_white= 2*ttail(e(df_r),abs(t_white))

xi: cluster2 rating i.real*male i.treated*male i.rXtreated*male i.wave*male i.wave if treatment >0 & norms==0 & norms2==0 , tcluster(id) fcluster(item_num)
gen t_male= _b[_IrXtXm]/_se[_IrXtXm]
gen p_male= 2*ttail(e(df_r),abs(t_male))



// bonferroni-holm corrections
drop tmp
gen tmp=_n
keep if tmp==1
keep p_* tmp
rename p_accimp p1
rename p_crt p2
rename p_cc p3
rename p_cn p4
rename p_age p5
rename p_white p6
rename p_att p7
rename p_dr p8
rename p_ed p9
rename p_male p10
reshape long p, i(tmp) j(hypothesis)

sort p
gen k=_n
gen p_holm=p*(10+1-k)